package com.dragon.backstage.config;

import com.dragon.backstage.interceptor.LoginInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置
 *
 * @author dragon
 * @version 1.0
 * @date 2024/7/26 16:51
 */
@Configuration
@ComponentScan(basePackages = {
    "com.dragon.common",
    "com.dragon.framework",
    "com.dragon.interfaces",
    "com.dragon.quartz"
})
@MapperScan(basePackages = {
    "com.dragon.interfaces.mapper"
})
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }


    /**
     * 配置本地资源映射路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // knife4j静态资源
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // 文件上传目录
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:" + System.getProperty("user.dir") + "/dragon-backstage/uploads/");
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*
         * 登陆拦截器
         * 仅作为参考示例
         */
        registry.addInterceptor(loginInterceptor())
            // 拦截所有URL
            .addPathPatterns("/**")
            /* 不拦截URL */
            .excludePathPatterns(
                // 网页图标
                "/favicon.ico",
                "/static/**",
                // springDoc
                "/swagger-ui/**",
                "/v3/api-docs/**",
                // knife4j
                "/doc.html",
                // 登陆模块接口
                "/api/captcha",
                "/api/login",
                "/api/logout"
            );
    }

}
